package com.csoa.community.services;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;

import com.csoa.community.singletons.GlobalVars;
import com.csoa.community.utils.Json;
import com.csoa.community.utils.StringUtils;

public class DownLoadAttachService extends Service {
	// 下载状态
	private final static int DOWNLOAD_COMPLETE = 0;
	private final static int DOWNLOAD_FAIL = 1;
	private Handler downLoadHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case DOWNLOAD_COMPLETE:
				Toast.makeText(DownLoadAttachService.this, "附件下载成功", Toast.LENGTH_LONG).show();
				break;
				
			case DOWNLOAD_FAIL:
				Toast.makeText(DownLoadAttachService.this, "附件下载失败", Toast.LENGTH_LONG).show();
				break;

			default:
				break;
			}
		}

	};

	public class DownLoadBinder extends Binder {
		public DownLoadAttachService getService() {
			return DownLoadAttachService.this;
		}
	}

	private String fileName;
	@SuppressWarnings("unused")
	private String downUrl;


	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		downUrl = intent.getStringExtra("downUrl");
		netRequestDownAttach();
		return super.onStartCommand(intent, flags, startId);
	}

//	protected void buildDownLoadAttachRequest(String AttachID,
//			String FileExtend) {
//		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
//		params.add(new BasicNameValuePair("AttachID", AttachID));
//		params.add(new BasicNameValuePair("FileExtend", FileExtend));
//		netRequestDownAttach(params);
//	}

	private void netRequestDownAttach() {
			

		new Thread() {
			@Override
			public void run() {
				String json = "";
				//					json = HttpUtils.getInst().getStringByWcf(
//							DataProcessor.OFFICIAL_DOC_DOWNLOAD_ATTACH_METHOD,
//							params);
				if (!StringUtils.isEmpty(json)) {
					String stadard = json.replace("[", "").replace("]", "");
					Json jsonstadard = new Json(stadard);
					String attachBody = jsonstadard.getString("A_body");

					File file = new File(GlobalVars.getInst()
							.getAttachPath() + fileName);
					
					DataOutputStream dos = null;
					try {
						if (!file.exists()) {
							file.createNewFile();
						}
						dos = new DataOutputStream(new FileOutputStream(
								file));
						dos.write(attachBody.getBytes());
						downLoadHandler.sendEmptyMessage(DOWNLOAD_COMPLETE);
					} catch (IOException e) {
						downLoadHandler.sendEmptyMessage(DOWNLOAD_FAIL);
						e.printStackTrace();
					} finally {
						if (dos != null) {
							try {
								dos.close();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}

					}

				}else {
					downLoadHandler.sendEmptyMessage(DOWNLOAD_FAIL);
				}

			}

		}.start();

	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

}
